package com.onlineMIS.action.headQ.custMgmt;

import java.io.InputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.ServletActionContext;
import org.springframework.beans.factory.annotation.Autowired;

import com.onlineMIS.ORM.DAO.Response;
import com.onlineMIS.ORM.DAO.headQ.barCodeGentor.HeadQTagDaoImpl;
import com.onlineMIS.ORM.DAO.headQ.barCodeGentor.HeadQTagGroupDaoImpl;
import com.onlineMIS.ORM.DAO.headQ.custMgmt.HeadQCustDaoImpl;
import com.onlineMIS.ORM.DAO.headQ.custMgmt.HeadQCustMgmtService;
import com.onlineMIS.ORM.entity.headQ.barcodeGentor.HeadQTag;
import com.onlineMIS.ORM.entity.headQ.barcodeGentor.HeadQTagGroup;
import com.onlineMIS.ORM.entity.headQ.custMgmt.HeadQCust;
import com.onlineMIS.common.loggerLocal;
import com.opensymphony.xwork2.ActionContext;

public class HeadQCustMgmtJSPAction extends HeadQCustMgmtAction {

	/**
	 * 
	 */
	private static final long serialVersionUID = -4965927215338212593L;

	
	@Autowired
	private HeadQCustMgmtService headQCustMgmtService;
	
	@Autowired
	private HeadQCustDaoImpl headQCustDaoImpl;
	@Autowired
	private HeadQTagDaoImpl headQTagDaoImpl;
	@Autowired
	private HeadQTagGroupDaoImpl tagGroupDaoImpl;
	
	private final String templateHeadCust = "HeadCustTemplate.xls";

	/**
	 * 进入list cust 页面
	 * @return
	 */
	public String preCust(){
		loggerLocal.info("HeadQSalesJSPAction.preCust");
		 //获取所有标签
	    List<HeadQTag> tags = headQTagDaoImpl.getAllCustTags();
		//获取分组
		List<Integer> groups = tagGroupDaoImpl.selectByGroup(HeadQTagGroup.GROUP_ONE);
		List<HeadQTag> tagLevelOne = new ArrayList<HeadQTag>();
		if (groups.size() >0 )
			tagLevelOne = headQTagDaoImpl.getLevelOne(groups.get(0));
		List<HeadQTag> tagLevelTwo = new ArrayList<HeadQTag>();
		if (groups.size() >1 )
			tagLevelTwo = headQTagDaoImpl.getLevelOne(groups.get(1));
		List<HeadQTag> tagLevelThree = new ArrayList<HeadQTag>();
		if (groups.size() >2 )
			tagLevelThree = headQTagDaoImpl.getLevelOne(groups.get(2));
		List<HeadQTag> tagLevelFour = new ArrayList<HeadQTag>();
		if (groups.size() >3 )
			tagLevelFour = headQTagDaoImpl.getLevelOne(groups.get(3));

		uiBean.setTagLevelOne(tagLevelOne);
		uiBean.setTagLevelTwo(tagLevelTwo);
		uiBean.setTagLevelThree(tagLevelThree);
		uiBean.setTagLevelFour(tagLevelFour);
	    uiBean.setTags(tags);
		return "listCust";
		
	}
	
	/**
	 * 准备添加或者修改某个客户
	 * @param headQCust
	 * @return
	 */
	public String preEditCust(){
		loggerLocal.info("HeadQSalesJSPAction.preEditCust");
		
		Response response = headQSalesService.preEditCust(formBean.getCust());
		if (response.isSuccess()){
			HeadQCust cust = (HeadQCust)response.getReturnValue();
			formBean.setCust(cust);
		} else 
			addActionError(response.getMessage());
		
		return "editCust";
	}


	/**
	 * 下载 客户往来账单据
	 * 
	 * @return
	 */
	public String downloadCustExcelReport() {
		loggerLocal.info(this.getClass().getName() + ".downloadAcctFlow");
		HttpServletRequest request = (HttpServletRequest) ActionContext.getContext()
				.get(ServletActionContext.HTTP_REQUEST);
		String contextPath = request.getRealPath("/");
		Response response = new Response();
		try {
			response = headQCustMgmtService
					.generateHeadCustExcel(contextPath + "WEB-INF\\template\\" + templateHeadCust, formBean);
		} catch (Exception e) {
			response.setReturnCode(Response.FAIL);
			response.setMessage(e.getMessage());
		}

		if (response.getReturnCode() == Response.SUCCESS) {
			InputStream excelStream = (InputStream) response.getReturnValue();
			formBean.setFileStream(excelStream);
			formBean.setFileName(templateHeadCust);
			return "download";
		} else
			return ERROR;
	}


}
